package HLLib.game;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLLibObject;
import HLCode.HLObject;
import HLLib.base.HLList;

public class HLSpriteInfoManagerOrder extends HLLibObject implements HLWorldScaled_H {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_GAME, HLClassManager_H.LIB_CLASS_GAME_SpriteInfoManagerOrder);
    }

    public HLObject GetObject(int index) {
        switch (index) {
        case 0: {
            return ownerWorld;
        }
        case 1: {
            return spriteInfos;
        }
        }
        return null;
    }

    public void SetObject(int index, HLObject value) {
        switch (index) {
        case 0: {
            ownerWorld = (HLWorld) value;
            return;
        }
        case 1: {
            spriteInfos = (HLList) value;
            return;
        }
        }
    }

    public HLWorld ownerWorld;

    public HLList spriteInfos;

    public HLSpriteInfoManagerOrder(HLWorld ownerWorld) {
        this.ownerWorld = ownerWorld;
    }

    public HLSpriteInfoManagerOrder() {
    }

    public void Clear() {
        spriteInfos = new HLList();
    }

    public void Refresh(HLList spriteInfos, int direct) {
        this.spriteInfos.Clear();

        for (int i = 0; i < spriteInfos.items.length; i++) {
            HLSpriteInfo info = (HLSpriteInfo) spriteInfos.items[i];

            int j = 0;
            for (; j < i; j++) {
                HLSpriteInfo tempInfo = (HLSpriteInfo) spriteInfos.items[j];
                if (direct == HLWorldScaled_H.DIRECT_E && info.gridX <= tempInfo.gridX || //
                        direct == HLWorldScaled_H.DIRECT_N && info.gridY >= tempInfo.gridY || //
                        direct == HLWorldScaled_H.DIRECT_S && info.gridY <= tempInfo.gridY || //
                        direct == HLWorldScaled_H.DIRECT_W && info.gridX >= tempInfo.gridX) {
                    break;
                }
            }

            this.spriteInfos.Insert(j, info);
        }
    }

    public HLList GetNewSpriteInfos(int rectX, int rectY, int rectWidth, int rectHeight) {
        HLList newList = new HLList();

        int rectGridX0 = rectX / ownerWorld.gridWidth;
        int rectGridY0 = rectY / ownerWorld.gridHeight;
        int rectGridX1 = (rectX + rectWidth - 1) / ownerWorld.gridWidth;
        int rectGridY1 = (rectY + rectHeight - 1) / ownerWorld.gridHeight;

        int count = 0;
        for (int i = 0; i < spriteInfos.items.length; i++) {
            HLSpriteInfo info = (HLSpriteInfo) spriteInfos.items[i];

            if (HLCDGroupAllTrans.RectTouchPoint1(rectGridX0, rectGridY0, rectGridX1, rectGridY1, info.gridX, info.gridY)) {
                count++;
            } else {
                break;
            }
        }

        if (count > 0) {
            for (int i = count - 1; i >= 0; i--) {
                newList.Add1(spriteInfos.items[i]);
            }

            spriteInfos.Resize(0, -count);
        }

        return newList;
    }
}
